from decimal import Decimal

from django.utils import timezone
from django_redis import get_redis_connection
from rest_framework import serializers, status
# from fjr.serializers.sku import SKUModelSerializer
from rest_framework.response import Response

from fjr.models import SKU
from fjr.serializers.sku import SKUModelSerializer
from orders.models import OrderInfo, OrderGoods, Payment
from users_lab.models import Address, User


class PaymentSerializer(serializers.ModelSerializer):
    trade_id = serializers.CharField(max_length=100, label="支付编号",read_only=True)

    class Meta:
        model = Payment
        fields = ('trade_id', )

class CommentGoodsSerializer(serializers.ModelSerializer):
    order = serializers.PrimaryKeyRelatedField(label='订单ID', write_only=True, queryset=OrderInfo.objects.all())
    sku = serializers.PrimaryKeyRelatedField(label='SKU',queryset=SKU.objects.all())
    class Meta:
        model = OrderGoods
        fields = ('order', 'sku', 'comment', 'score', 'is_anonymous')
        # exclude = ('count', 'price')

class UnCommentGoodSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'comments', 'default_image')
        read_only_fields = ('id', 'name', 'price', 'comments', 'default_image')


class UnCommentGoodsSerializer(serializers.ModelSerializer):
    id = serializers.PrimaryKeyRelatedField(label='ID', read_only=True)
    price = serializers.DecimalField(max_digits=10, decimal_places=2, default=0, label="单价",read_only=True)
    count = serializers.IntegerField(default=1, label="数量",read_only=True)
    sku = UnCommentGoodSerializer(label='商品信息',read_only=True)
    class Meta:
        model = OrderGoods
        fields = ('id', 'price', 'count', 'sku')
        extra_kwargs = {'sku': {'read_only': True}}


class OrderGoodsSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField(default=1, label="数量", read_only=True)
    class Meta:
        model = OrderGoods
        fields = ('count', )

# def count_data():
#     redis_conn = get_redis_connection('carts')
#     item_dict = redis_conn.hgetall('carts_')
#     cart_selected = redis_conn.smembers('selected_')
#     cart = {}
#     print(type(cart_selected))
#     for sku_id in cart_selected:
#         cart[int(sku_id)] = int(item_dict[sku_id])
#         print(sku_id)
#     return cart
# JacksonJsonRedisSerializer

class OrderSettleOrderGoodsSerializer(serializers.ModelSerializer):
    # sku = SKU.objects.get(id=1)
    # print(sku)
    # print(type(sku))
    # cart = count_data()
    price = serializers.DecimalField(max_digits=10, decimal_places=2, default=0, label="单价",read_only=True)
    # comments = serializers.IntegerField(default=0, label='评价数', read_only=True)
    id = serializers.PrimaryKeyRelatedField(label='ID', read_only=True)
    name = serializers.CharField(max_length=50, label='名称',read_only=True)
    count = serializers.IntegerField(label='count',read_only=True)

    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'default_image','count')


class OrderSettleSerializer(serializers.Serializer):
    skus = OrderSettleOrderGoodsSerializer(label='skus',many=True)
    freight = serializers.DecimalField(max_digits=10, decimal_places=2, default=10.00, label="运费",read_only=True)
    # class Meta:
    #     model = OrderInfo
    #     fields = ('freight', 'skus')

    # user = User.objects.get(is_staff=1)
    #
    # from rest_framework_jwt.settings import api_settings
    #
    # jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
    # jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
    #
    # payload = jwt_payload_handler(user)
    # token = jwt_encode_handler(payload)
    # print(token)

class OrderCommitSKUSerializer(serializers.ModelSerializer):
    price = serializers.DecimalField(max_digits=10, decimal_places=2, default=0, label="单价",read_only=True)
    id = serializers.PrimaryKeyRelatedField(label='ID', read_only=True)
    name = serializers.CharField(max_length=50, label='名称',read_only=True)
    count = serializers.IntegerField(label='count',read_only=True)

    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'count')


class OrderCommitSerializer(serializers.ModelSerializer):
    PAY_METHOD_CHOICES = (
        (1, "货到付款"),
        (2, "支付宝"),
    )
    address = serializers.PrimaryKeyRelatedField(label='收货地址',write_only=True, queryset=Address.objects.all())
    pay_method = serializers.ChoiceField(choices=PAY_METHOD_CHOICES, default=1, label='支付方式')
    order_id = serializers.CharField(label="订单编号", read_only=True)
    total_amount = serializers.DecimalField(label='总数量', max_digits=10, decimal_places=2, read_only=True)
    freight = serializers.DecimalField(label='运费',max_digits=10, decimal_places=2,read_only=True)
    skus = OrderCommitSKUSerializer(label='商品信息',read_only=True)

    class Meta:
        model = OrderInfo
        fields = ('address', 'order_id', 'total_amount', 'freight',  'pay_method', 'status','create_time','skus')

    def validate(self, attrs):
        pass

    def create(self, validated_data):
        super().create(validated_data)
        order_id = validated_data.order_id
        OrderInfo.objects.create(
            order_id=order_id
        )

        return Response(order_id)

class OrderIdRetrieveSerializer(serializers.ModelSerializer):
    PAY_METHOD_CHOICES = (
        (1, "货到付款"),
        (2, "支付宝"),
    )
    user_id = serializers.CharField(max_length=64, label="订单编号")
    address = serializers.PrimaryKeyRelatedField(label='收货地址', write_only=True, queryset=Address.objects.all())
    # address = serializers.IntegerField(label='地址')
    pay_method = serializers.ChoiceField(choices=PAY_METHOD_CHOICES, default=1, label="支付方式",write_only=True)
    order_id = serializers.CharField(max_length=64, label="订单编号",read_only=True)
    class Meta:
        model = OrderInfo
        fields = ('address', 'pay_method', 'order_id','user_id')

    # def validate(self, attrs):
    #     return self.initial_data